package com.bfb.helpers;

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.bfb.Main;

/**
 * Helper class for functionalities related to the restaurants that require
 * server contact.
 * 
 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
 * 
 */
public class Restaurants {

	/**
	 * Contacts the server by using an {@link HttpGet} with the url path being
	 * "/restaurants/all". The response is received in an {@link HttpResponse}
	 * that is a {@link JSONArray} containing the list of all the restaurants in
	 * the db. The result is returned as an {@link ArrayList} of all the
	 * restaurant names.
	 * 
	 * @return {@link ArrayList}
	 * 
	 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	 * 
	 */
	public static ArrayList<String> getAll() {
		ArrayList<String> restaurantNames = new ArrayList<String>();
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet("http://" + Main.SERVER
					+ "/restaurants/all");
			HttpResponse response = httpClient.execute(httpGet);
			if (response != null) {
				HttpEntity entity = response.getEntity();
				JSONArray jsonArray = new JSONArray(
						ServerUtilities.getContentFromStream(entity
								.getContent()));
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					restaurantNames.add(jsonObject.getString("name"));
				}
			} else
				throw new Exception("Response NULL");
		} catch (Exception e) {
			Log.e("Restaurants_GetAll", e.getMessage(), e);
		}
		return restaurantNames;
	}

	/**
	 * Contacts the server by using an {@link HttpPost} with the url path being
	 * "/restaurants/get_key" to retrieve the key of a restaurant by its name.
	 * The name is posted in a list of {@link NameValuePair}. The repsponse is
	 * then retrieved and returned.
	 * 
	 * @param name
	 *            The name of the restaurant
	 * 
	 * @return String
	 * 
	 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	 */
	public static String getKeyByName(final String name) {
		String key = "";
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost("http://" + Main.SERVER
					+ "/restaurants/get_key");

			ArrayList<NameValuePair> nvpList = new ArrayList<NameValuePair>();
			nvpList.add(new BasicNameValuePair("restaurantName", name));
			httppost.setEntity(new UrlEncodedFormEntity(nvpList));

			HttpResponse response = httpClient.execute(httppost);

			if (response != null) {
				HttpEntity entity = response.getEntity();
				key = ServerUtilities.getContentFromStream(entity.getContent());
			} else
				throw new Exception("Response NULL");
		} catch (Exception e) {
			Log.e("Restaurant ID Request", e.getMessage(), e);
		}

		return key;
	}
}
